﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using VIBlend.WPF.Controls;
using System.Globalization;
using System.ComponentModel;

namespace AnonsaiWPF.CSharp
{
    /// <summary>
    /// Interaction logic for DateTimeEditor.xaml
    /// </summary>
    public partial class DateTimeEditor : Window, IWindow
    {
        private ErrorProvider provider = new ErrorProvider();

        public void ClearProvider()
        {
            this.provider.Clear();
        }

        public Grid Root
        {
            get
            {
                return this.RootVisual;
            }
        }

        public void DetachContent()
        {
            RemoveLogicalChild(Content);
        }

        public DateTimeEditor()
        {
            InitializeComponent();

            this.Alignment.Items.Add("TopLeft");
            this.Alignment.Items.Add("TopRight");
            this.Alignment.Items.Add("MiddleLeft");
            this.Alignment.Items.Add("MiddleRight");
            this.Alignment.Items.Add("BottomLeft");
            this.Alignment.Items.Add("BottomRight");
            this.Alignment.SelectionChanged += new SelectionChangedEventHandler(Alignment_SelectionChanged);


            this.Minimum.TextChanged += new TextChangedEventHandler(Minimum_TextChanged);
            this.Maximum.TextChanged += new TextChangedEventHandler(Maximum_TextChanged);
            this.LayoutRoot.Children.Add(provider);

            this.ShortDate.ValueChanged += new EventHandler<DateTimeValueEventArgs>(Editor_ValueChanged);
            this.LongDate.ValueChanged += new EventHandler<DateTimeValueEventArgs>(Editor_ValueChanged);
            this.FullDateTimePattern.ValueChanged += new EventHandler<DateTimeValueEventArgs>(Editor_ValueChanged);
            this.CustomFormat.ValueChanged += new EventHandler<DateTimeValueEventArgs>(Editor_ValueChanged);

            DateTime maxValue = new DateTime(2100, 1, 1);
            DateTime minValue = new DateTime(1900, 1, 1);

            this.NullDate.Value = null;
            this.NullDate.Minimum = minValue;
            this.NullDate.Maximum = maxValue;

            this.NullDate.ValueChanged += new EventHandler<DateTimeValueEventArgs>(Editor_ValueChanged);
            this.ShortDate.Minimum = minValue;
            this.ShortDate.Maximum = maxValue;

            this.LongDate.Minimum = minValue;
            this.LongDate.Maximum = maxValue;
         
            this.LongTime.Minimum = minValue;
            this.LongTime.Maximum = maxValue;

            this.FullDateTimePattern.Minimum = minValue;
            this.FullDateTimePattern.Maximum = maxValue;

            this.CustomFormat.Minimum = minValue;
            this.CustomFormat.Maximum = maxValue;

            this.ShortTime.Minimum = minValue;
            this.ShortTime.Maximum = maxValue;

            this.Maximum.Text = maxValue.ToShortDateString();
            this.Minimum.Text = minValue.ToShortDateString();

            this.CultureInfoBox.Items.Add("en-US");
            this.CultureInfoBox.Items.Add("en-GB");
            this.CultureInfoBox.Items.Add("he-IL");
            this.CultureInfoBox.Items.Add("fr-FR");
            this.CultureInfoBox.Items.Add("de-DE");
            this.CultureInfoBox.SelectionChanged += new SelectionChangedEventHandler(CultureInfoBox_SelectionChanged);
      
            this.MinHeight = 500;
            this.MinWidth = 735;
        }

        void CultureInfoBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            CultureInfo info = new CultureInfo(this.CultureInfoBox.SelectedItem.ToString());

            VIBlend.WPF.Controls.DateTimeEditor editor = new VIBlend.WPF.Controls.DateTimeEditor();
            editor.Culture = new CultureInfo("de-DE");

            this.FullDateTimePattern.Culture = info;
            this.ShortTime.Culture = info;
            this.LongTime.Culture = info;
            this.LongDate.Culture = info;
            this.ShortDate.Culture = info;
            this.CustomFormat.Culture = info;
        }

        void Editor_ValueChanged(object sender, DateTimeValueEventArgs e)
        {
            VIBlend.WPF.Controls.DateTimeEditor editor = sender as VIBlend.WPF.Controls.DateTimeEditor;
            if (editor == null)
                return;

            if (editor.Value < editor.Minimum)
            {
                this.provider.SetError(editor, "Value is less than the Minimum value");
            }
            else if (editor.Value > editor.Maximum)
            {
                this.provider.SetError(editor, "Value is greater than the Maximum value!");
            }
            else this.provider.SetError(editor, "");
        }

        void Maximum_TextChanged(object sender, TextChangedEventArgs e)
        {
            DateTime result;
            if (DateTime.TryParse(this.Maximum.Text, out result))
            {
                this.ShortDate.Maximum = result;
            }
            else
            {
                DateTime maxValue = new DateTime(2100, 1, 1);

                this.Maximum.Text = maxValue.ToShortDateString();
            }
        }

        void Minimum_TextChanged(object sender, TextChangedEventArgs e)
        {
            DateTime result;
            if (DateTime.TryParse(this.Minimum.Text, out result))
            {
                this.ShortDate.Minimum = result;
            }
            else
            {
                DateTime minValue = new DateTime(1900, 1, 1);
                this.Minimum.Text = minValue.ToShortDateString();
            }
        }

        void Alignment_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            switch (this.Alignment.SelectedIndex)
            {
                case 0:
                    provider.IconAlignment = ErrorProviderIconAlignment.TopLeft;
                    break;
                case 1:
                    provider.IconAlignment = ErrorProviderIconAlignment.TopRight;
                    break;
                case 2:
                    provider.IconAlignment = ErrorProviderIconAlignment.MiddleLeft;
                    break;
                case 3:
                    provider.IconAlignment = ErrorProviderIconAlignment.MiddleRight;
                    break;
                case 4:
                    provider.IconAlignment = ErrorProviderIconAlignment.BottomLeft;
                    break;
                case 5:
                    provider.IconAlignment = ErrorProviderIconAlignment.BottomRight;
                    break;
            }
        }

        private void RadioButton_Checked(object sender, RoutedEventArgs e)
        {
            if (this.BlinkIfDifferent == null)
                return;

            if (this.BlinkIfDifferent.IsChecked.Value)
            {
                this.provider.BlinkStyle = ErrorProviderBlinkStyle.BlinkIfDifferentError;
            }
            else if (this.NeverBlink.IsChecked.Value)
            {
                this.provider.BlinkStyle = ErrorProviderBlinkStyle.NeverBlink;
            }
            else if (this.AlwaysBlink.IsChecked.Value)
            {
                this.provider.BlinkStyle = ErrorProviderBlinkStyle.AlwaysBlink;
            }
        }


        public void SetTheme()
        {
        }
  
    }
}
